package com.simon.netty.demo1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Created by changf on 2017/7/13.
 */
public class EchoServer {

    private int port;

    EchoServer(int port){
        this.port=port;
    }


    public  void start() throws  Exception{
        NioEventLoopGroup nioEventLoopGroup=new NioEventLoopGroup();
        ServerBootstrap serverBootstrap=new ServerBootstrap();
        serverBootstrap.group(nioEventLoopGroup).channel(NioServerSocketChannel.class).localAddress("localhost",port)
                .childHandler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        channel.pipeline().addLast(new EchoServer1InHandler());
                        channel.pipeline().addLast(new EchoServerOut1handler());
                        channel.pipeline().addLast(new EchoServerOut2Handler());
                        channel.pipeline().addLast(new EchoServer2InHandler());

                    }
                });

        ChannelFuture channelFuture = serverBootstrap.bind().sync();
        System.out.println("开始监听 端口："+channelFuture.channel().localAddress());
        channelFuture.channel().closeFuture().sync();
    }

    public static void main(String[] args) throws Exception{
        new EchoServer(20000).start();
    }
}
